package org.jianfa.learn.transfer.utils;

import java.lang.reflect.Method;

public class MethodUtil {

    public static String getMethodIdentifier(Method method) {
        StringBuilder sb = new StringBuilder();
        String methodName = method.getName();
        sb.append(methodName);
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (int i=0; i<parameterTypes.length; i++) {
            Class parameterType = parameterTypes[i];
            String parameterTypeName = parameterType.getName();
            if (parameterTypeName.contains(".")) {
                parameterTypeName = parameterTypeName.substring(parameterTypeName.lastIndexOf(".") + 1);
            }
            sb.append("+").append(parameterTypeName);
        }

        Class returnType = method.getReturnType();
        String returnTypeName = returnType.getTypeName();
        if (returnTypeName.contains(".")) {
            returnTypeName = returnTypeName.substring(returnTypeName.lastIndexOf(".") + 1);
        }
        sb.append("+").append(returnTypeName);
        return sb.toString();
    }
}
